package com.education.ai.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

@Component
public class WebClientConfig {

    @Bean
    @Qualifier("customQwenWebClient")
    public WebClient customQwenWebClient(@Value("${qwen.api.uri:https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation}") String apiUri,
                                   @Value("${qwen.api.key:your-api-key-here}") String apiKey) {
        
        return WebClient.builder()
            .baseUrl(apiUri)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
            .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) // 16MB
            .build();
    }
    
    @Bean
    @Qualifier("tongYiQwenWebClient")
    public WebClient tongYiQwenWebClient(@Value("${tongyi.qwen.api.uri:https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation}") String apiUri,
                                         @Value("${tongyi.qwen.api.key:your-api-key-here}") String apiKey) {
        
        return WebClient.builder()
            .baseUrl(apiUri)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
            .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) // 16MB
            .build();
    }
    
    @Bean
    @Qualifier("tongYiVisionWebClientCustom")
    public WebClient tongYiVisionWebClientCustom(@Value("${tongyi.vision.api.uri:https://dashscope.aliyuncs.com/api/v1/services/aigc/text-to-image/generation}") String apiUri,
                                           @Value("${tongyi.vision.api.key:your-api-key-here}") String apiKey) {
        
        return WebClient.builder()
            .baseUrl(apiUri)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
            .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) // 16MB
            .build();
    }
} 